package cart;

import cart.hibernate.member.ManageMember;
import cart.Authentication;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class Register extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/json; charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        out.print("{\"success\":");

        try {
            ManageMember manager = new ManageMember();

            String loginName = request.getParameter("user_id");
            String password = request.getParameter("user_pwd1");
            Authentication user = new Authentication(loginName,password);
            password = user.hashPwd();
            String email = request.getParameter("email");
            char gender = request.getParameter("gender").charAt(0);
            
            String homePhoneNumber = request.getParameter("hometel");
            String cellPhoneNumber = request.getParameter("celltel");

            Date birthDate = new SimpleDateFormat("dd-MM-yyyy").parse(request.getParameter("DOB"));

            Date joinDate = new Date();

            //Date joinDate = Date.parse(dateTemp);
            String nickName = request.getParameter("nickname");
            long point = 0;
            String firstName = request.getParameter("firstname");
            String middleName = request.getParameter("midname");
            String lastName = request.getParameter("lastname");
            long membershipTierId = 1;


            String newLoginName = manager.addMember( loginName, password, email, gender, 
                                                homePhoneNumber, cellPhoneNumber, birthDate, 
                                                nickName, point, firstName, 
                                                middleName, lastName, membershipTierId);
            out.print("1");
        } catch (ParseException ex) {
            Logger.getLogger(Register.class.getName()).log(Level.SEVERE, null, ex);
            out.print("0");
        }
        out.print("}");
        out.close();
    }
   

}
